# coding: utf-8
# @Author : lryself
# @Date : 2020/12/14 11:46
# @Software: PyCharm
from ..models import db
from ..models.InformationInfoModel import InformationInfo
from ..utils import commons
from ..utils.response_code import RET


class InformationInfoController(InformationInfo):
    @classmethod
    def get_list(cls, **kwargs):
        filter_list = []
        if kwargs.get("InformationID"):
            filter_list.append(cls.informationID == kwargs.get("InformationID"))
        if kwargs.get("Title"):
            filter_list.append(cls.title.contains("Title"))
        if kwargs.get("Text"):
            filter_list.append(cls.text.contains("Text"))
        if kwargs.get("ParentCodeID") is not None:
            filter_list.append(cls.parentCodeID == kwargs.get("ParentCodeID"))

        try:
            result_model = db.session.query(cls.informationID, cls.title, cls.text, cls.parentCodeID).filter(*filter_list)
            if not result_model.first():
                return {'code': RET.NODATA, 'msg': '无结点信息', 'error': '无结点信息'}
        except Exception as e:
            return {'code': RET.DBERR, 'msg': '数据库异常，查询用户信息失败', 'error': str(e)}

        return {'code': RET.OK, 'msg': '查询成功！', 'data': commons.query_to_dict(result_model.all())}

    @classmethod
    def get(cls, **kwargs):
        filter_list = []
        if kwargs.get("InformationID"):
            filter_list.append(cls.informationID == kwargs.get("InformationID"))
        if kwargs.get("Title"):
            filter_list.append(cls.title.contains("Title"))
        if kwargs.get("Text"):
            filter_list.append(cls.text.contains("Text"))
        if kwargs.get("ParentCodeID") is not None:
            filter_list.append(cls.parentCodeID == kwargs.get("ParentCodeID"))

        try:
            result_model = db.session.query(cls.informationID, cls.title, cls.text, cls.parentCodeID).filter(*filter_list)
            if not result_model.first():
                return {'code': RET.NODATA, 'msg': '无结点信息', 'error': '无结点信息'}
        except Exception as e:
            return {'code': RET.DBERR, 'msg': '数据库异常，查询用户信息失败', 'error': str(e)}

        return {'code': RET.OK, 'msg': '查询成功！', 'data': commons.query_to_dict(result_model.first())}